$.extend(validatePrompt, {
    mail:{
        onFocus:"",
        succeed:"",
        isNull:"Please enter your e-mail address.",
        error:"Please enter a valid e-mail address."
    }
});

$.extend(validateFunction, {
    mail:function(option) {
        var format = validateRules.isNull(option.value);
        var format2 = validateRules.isEmail(option.value);
        if (!format&&format2) {
            validateSettings.succeed.run(option);
        }
        else {
            if(format){
                validateSettings.error.run(option, option.prompts.isNull);
            }

            else if(!format2)
                validateSettings.error.run(option, option.prompts.error);
        }
    },
    pwd:function(option) {
        validateSettings.succeed.run(option);
    },
    FORM_validate:function() {
        $("#loginname").Validate(validatePrompt.loginname,validateFunction.loginname,true);
        $("#loginpwd").Validate(validatePrompt.pwd, validateFunction.pwd, true);
        return validateFunction.FORM_submit(["#loginname","#loginpwd"]);
    },
    FORM_validate_authcode:function() {
        $("#loginname").Validate(validatePrompt.mail,validateFunction.mail,true);
        $("#loginpwd").Validate(validatePrompt.pwd, validateFunction.pwd, true);
        $("#lauthcode").Validate(validatePrompt.authcode, validateFunction.authcode, true);
        return validateFunction.FORM_submit(["#loginname","#loginpwd","#lauthcode"]);
    }
});
//setTimeout(function(){$("#loginname").get(0).focus();},0);
setTimeout(function() {
    if (!$("#loginname").val()) {
        $("#loginname").get(0).focus();
    }
}, 0);
$("#loginname").Validate(validatePrompt.loginname,validateFunction.loginname);
$("#loginpwd").Validate(validatePrompt.empty, validateFunction.pwd);
$("#lauthcode").Validate(validatePrompt.empty, validateFunction.authcode);



$("#loginname,#loginpwd, #lauthcode").bind('keyup', function(event) {
    if (event.keyCode == 13) {
        $("#loginsubmit").click();
    }
});

$("#loginsubmit").click(function() {
	var flag = validateFunction.FORM_validate();
	if(flag){
		var username = $("#loginname").attr("value");
		var password = $("#loginpwd").attr("value");
		
		var format = validateRules.isEmail(username);
		if(format){
			var json = {"email": username, "password": password};
		}else{
			var json = {"userName": username, "password": password};
		}
		
		
	    $(this).attr({ "disabled": "disabled" });
	    $.ajax({
	        type: "POST",
	        url: "/RemoteSensing/user/signin.action?random=" + Math.random(),
	        dataType: "json",
	        data: JSON.stringify(json),
	        contentType:"application/json",
	        error: function() {
	            $("#loginpwd").attr({ "class": "s-textbox highlight2" });
	            $("#loginpwd_error").html("系统繁忙！").show().attr({ "class": "error" });
	            $("#loginsubmit").removeAttr("disabled");
	        },
	        success: function(result) {
	        	if(result.success == "SUCCESS"){
	        		var url;
	        		if(document.referrer == ""){
	        			url = '/RemoteSensing/category/home.html';
	        		}else{
	        			url = document.referrer;
	        		}
	        		window.location.replace(url);	
	        	}else{
	        		$("#loginpwd_error").html("用户名或密码错误！").show().attr({ "class": "error" });
	        		$("#loginpwd").attr("value", "")
	        		$("#loginsubmit").removeAttr("disabled");
	        	}
	        	
	            /*if (result) {
	                var obj = eval(result);
	                if (obj.success) {
	                    if (typeof SSO == "undefined") {
	                        window.location = obj.success;
	                    } else {
	                        SSO.login(function(url) {
	                            window.location = url;
	                        }, obj.success);
	                    }
	                }
	                if (obj.transfer) {
	                    if (typeof SSO == "undefined") {
	                        window.location = obj.transfer + window.location.search;
	                    } else {
	                        SSO.login(function(url) {
	                            window.location = url;
	                        }, obj.transfer + window.location.search);
	                    }
	                }
	                if (obj.verifycode)
	                    $("#o-authcode").show();
	                if (obj.username) {
	                    $("#loginname").attr({ "class": "s-textbox highlight2" });
	                    $("#loginname_error").html(obj.username).show().attr({ "class": "error" });
	                }
	                if (obj.pwd) {
	                    $("#loginpwd").attr({ "class": "s-textbox highlight2" });
	                    $("#loginpwd_error").html(obj.pwd).show().attr({ "class": "error" });
	                }
	                if (obj.authcode) {
	                    $("#lauthcode").attr({ "class": "s-yzbox highlight2" });
	                    $("#lauthcode_error").html(obj.authcode).show().attr({ "class": "error" });
	                }
	                $("#loginsubmit").removeAttr("disabled");
	                verc1();
	            }*/
	        }
	    });
	}
	
});


$("#loginname,#loginpwd, #authcode").bind('keyup', function(event) {
    if (event.keyCode == 13) {
        $("#loginsubmit").click();
    }
});